/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.starter.integration.module.web;

import org.apache.commons.lang3.StringUtils;
import org.pajamas.daydream.template.sequence.SequenceTemplate;
import org.pajamas.starter.integration.module.context.PajamasPropSrcConstants;
import org.pajamas.starter.integration.module.context.PajamasSequenceAutoConfiguration;
import org.pajamas.starter.integration.spring.aop.AspectJAdvisorBuilder;
import org.springframework.aop.Advisor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * @author william
 * @since 2024/4/15
 */
@ConditionalOnProperty(prefix = "org.pajamas.starter.web.validation", name = "enabled", havingValue = "true",
        matchIfMissing = true)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableConfigurationProperties(PajamasValidationProperties.class)
@Import(PajamasExceptionHandlerAdvice.class)
@AutoConfigureAfter(PajamasSequenceAutoConfiguration.class)
public class PajamasBlockingValidationAutoConfiguration {
    private static final String SEQUENCE_PATTERN_ASPECTJ_EXPRESSION =
            "execution (* {" + PajamasPropSrcConstants.BASE_PACKAGE_APP + "}..controller.*Controller.*(..))";

    @Bean
    public PajamasMethodValidationAdvice pajamasMethodValidationAdvice() {
        return new PajamasMethodValidationAdvice();
    }

    @Bean
    @ConditionalOnBean(PajamasMethodValidationAdvice.class)
    public Advisor pajamasValidationAdvisor(PajamasMethodValidationAdvice pajamasMethodValidationAdvice,
                                            PajamasValidationProperties properties, SequenceTemplate sequenceTemplate) {
        String defaultAspectJExpression = sequenceTemplate.create(SEQUENCE_PATTERN_ASPECTJ_EXPRESSION).interpretSelf();
        return AspectJAdvisorBuilder
                .newBuilder()
                .pointcut(StringUtils.firstNonBlank(
                        properties.getAspectjExpression(),
                        defaultAspectJExpression))
                .advice(pajamasMethodValidationAdvice)
                .build();
    }
}
